/*********** NOTES HEADER *********** 

DESCRIPTION: This program ensures the diabetes outcome and merger timing 
 variables are on the relevant analysis dataset.
 
 Datasets used:
  (1) vertmergers
  (2) Bene_doc_match
  (3) fullListAnalysis2005-2012
  (4) tempdiabcompl2005_2012
  
 Datasets created:
  (1) fullListAnalysis2005-2012
    
************************************/ 

set more off
capture log close
clear all
global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients\"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2
adopath +  N:\SIL-Common\reghdfe-master\package

***Merger timing 

		tempfile timing

		use "$dataIn/vertmergers"
			gen vm_provider1 = vm_PROVIDER1
			gen vm_provider2 = vm_PROVIDER1
			gen vm_provider3 = vm_PROVIDER1

		drop vm_PROVIDER1
		  rename 	q_of_merger qom
		  save `timing' 

		clear

***End merger timing 



	tempfile fsdata
	 use BENE_ID year firmsize fs* doc* using "$dataOut/Bene_doc_match.dta" 
	save `fsdata'
	clear
 

	use "$dataOut/fullListAnalysis2005-2012.dta" 
	*this drops the non-merged from previous program
		drop if _merge==2
		capture drop year
		capture drop _merge
		gen year=year(quarter)
		sort BENE_ID year
		merge m:1 BENE_ID year using `fsdata'
	drop if _merge==2
	capture drop _merge

	sort BENE_ID quarter
		foreach i of varlist  u* hs* hosp* {
			 by BENE_ID : replace `i' =`i'[_n-1] if `i'==. & _n>1
		}

		capture drop qom
		capture drop vm_PROVIDER*
		capture drop _merge
			foreach var of varlist vm_provider* {
				sort `var'
				merge m:1 `var' using `timing'
				drop if _merge==2
				drop _merge
				replace q_of_merger = qom if q_of_merger==.
				drop qom
			}

		egen newmerger= max(merger_seen), by(BENE_ID)
		drop merger_seen
		rename newmerger  merger_seen
		format q_of_merger %td
		*80% of the 150 observations are in 2005. All are missing ICD9 information
		drop if q_of_merger~=. & merger_seen==.

		egen qom = min(q_of_merger), by(BENE_ID)
		replace q_of_merger=qom if q_of_merger==.
		drop qom

			foreach var of varlist m* e* x* fs* pop_density {
				di "`var'"
				by BENE_ID: replace `var'=`var'[_n-1] if `var'==. & _n~=1

			}

		 replace mxicd8_chronic_p_all=1 if mxhypertension_p_all==1
		 replace mxicd3_chronic_p_all=1 if mxdiabetes_p_all==1

		save  "$dataOut/fullListAnalysis2005-2012.dta" , replace
			capture drop _merge
			sort BENE_ID quarter 
		merge 1:1 BENE_ID quarter using "$dataOut/tempdiabcompl2005_2012"

		sort BENE_ID quarter 
			foreach var of varlist diab* {
				 di "`var'"
				 by BENE_ID: replace `var'=`var'[_n-1] if `var'==. & _n~=1
			}
			foreach var of varlist diab* {
				 di "`var'"
				 capture drop n`var'
				 gen n`var'=`var'
				 by BENE_ID: replace n`var'=1 if n`var'[_n-1]==1  
			}

		drop if _merge==2

		replace mxdiab_compl1_p_all=ndiab_compl1 if mxdiab_compl1_p_all!=.
		replace mxdiab_compl2_p_all=ndiab_compl2 if mxdiab_compl2_p_all!=.
		  
		  drop diab* ndiab*
		  
		save "$dataOut/fullListAnalysis2005-2012.dta" , replace


